package com.shike.palmrejection;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class LockScreenActivity extends Activity {

    private static int time = 0;
    private static BroadcastReceiver endReceiver;
    public static boolean isTop = false;
    private Thread mThread;
    private static int black_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_lock_screen);

        Log.d("保活", "创建Activity " + ++time);

        // 在锁屏之上
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        //设置1像素
        Window window = getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 1;
        params.width = 1;
        window.setAttributes(params);

        //结束该页面的广播
        endReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                isTop = false;
                moveTaskToBack(true);
                --time;
            }
        };
        registerReceiver(endReceiver, new IntentFilter("closeLSB"));

//        showFloatingWindow();

        isTop = true;

        black_time = Integer.parseInt(SharepreferenceUtils.getString(this, Config.MILLISECOND, "1000"));
//        startBack();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        black_time = Integer.parseInt(SharepreferenceUtils.getString(this, Config.MILLISECOND, "1000"));
        Log.d("保活", "不是新建" + ++time);
        isTop = true;

//        startBack();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("保活", "关闭保活" + --time);
        isTop = false;
        unregisterReceiver(endReceiver);
        black_time = -100;
    }

    @Override
    public void onBackPressed() {
        // 进制返回
    }

//    private void startBack() {
//        if (mThread == null || mThread.isAlive()) {
//            mThread = new Thread(){
//                @Override
//                public void run() {
//
//                    while (true) {
//                        try {
//                            sleep(1);
//                            Log.d("TAG", "run: " + black_time);
//                            if (black_time-- <= 0) {
//                                handler.obtainMessage().sendToTarget();
//                                return;
//                            }
//
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                }
//            };
//
//            mThread.start();
//        }
//    }
//
//    @SuppressLint("HandlerLeak")
//    private Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            mThread = null;
//            if (black_time > -10) {
//                // 黑屏
//                Log.d("TAG", "handleMessage: 执行");
//                screenOff();
//            }
//        }
//    };

    private void screenOff(){
        DevicePolicyManager policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        policyManager.lockNow();
    }
}
